package cn.chainfind.cbc;

import cn.chainfind.web.domain.EasyLoanCompany;
import cn.chainfind.web.domain.CompanyResult;
import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.github.crab2died.ExcelUtils;
import com.github.crab2died.exceptions.Excel4JException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广州信易贷 工作专班
 * https://www.xinyidaigz.com/
 */
public class EasyLeanUtil {
    public static void main(String[] args) {
        spiderWeb();
    }

    private static void spiderWeb(){
        int dataRowNum=-1;
        int startPage = 1;
        int pageSize = 100;
        List<EasyLoanCompany> datas = new ArrayList<>();
        do {
            List<EasyLoanCompany> tmpData = collectData(startPage,pageSize);
            datas.addAll(tmpData);
            dataRowNum = tmpData.size();
            startPage++;

        }while (dataRowNum>=pageSize);
        exportData2Excel(datas);
    }



    public static List<EasyLoanCompany> collectData(int page, int pageSize){
        String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTY5MjQyNTg0MiIsImNyZWF0ZWQiOjE3MzYzOTE1OTE0NTF9.FvpbZSK-EAsqAXrC_vZxfmc1wUQpBMxR073-Yft6GE6VVTe6U4wTPPFJd2HQpcZLyQHX1XHeuZvzgVI7xglf_g";
        String url = "https://www.xinyidaigz.com/gw98/user/apply/rzsq/admin/list";
        Map<String,Object> params = new HashMap<>();
        params.put("pageNum", page);
        params.put("pageSize", pageSize);
//        params.put("isHasNeed", true);//是否有需求
//        params.put("checkStatus", "COM_SUBMIT");
//        params.put("isDeclare", "true");// 是否纳入申报
        String json = JSONUtil.toJsonStr(params);
        //链式构建请求
        String result2 = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
                .header(Header.ACCEPT, "application/json, text/plain, */*")
                .header(Header.ACCEPT_ENCODING, "gzip, deflate, br, zstd")
                .header(Header.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9")
                .header(Header.AUTHORIZATION, "Bearer "+token)
                .header(Header.CONNECTION, "keep-alive")
                .header(Header.CONTENT_LENGTH, "71")
                .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
                .header(Header.COOKIE, "acw_tc=ac11000117361695715045560e0039caad11ed0a4e5315c60d805cc93bc493; Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTY5MjQyNTg0MiIsImNyZWF0ZWQiOjE3MzYxNjk2MDMyMzh9.4AI-WRRyHpr12NPgHFDP_-bpXO0UH2iC3vSAleN1J5sOpxxvpM675OAl3a2czfVceACWvDgdS3leQAVUkVonaQ; Admin-UserName=15692425842; sidebarStatus=1")
                .header(Header.HOST, "www.xinyidaigz.com")
                .header(Header.ORIGIN, "https://www.xinyidaigz.com")
                .header(Header.REFERER, "https://www.xinyidaigz.com/admin/")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-origin")
                .header("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "Windows")
                .body(json)
                .timeout(20000)
                .execute().body();
        Console.log("开始打印第{}页",page);
        Console.log(result2);
        JSON json1= JSONUtil.parseObj(result2);
        CompanyResult companyResult = json1.toBean(CompanyResult.class);
        List<EasyLoanCompany> reCORDS = companyResult.getData().getRecords();
        return reCORDS;
    }



    private static void exportData2Excel(List<EasyLoanCompany> datas){
//        File file = new File("/Users/zcb/Work/IdeaProjects/WorkDay/easy_lean.json");
//        JSON dataJson = JSONUtil.readJSON(file, CharsetUtil.CHARSET_UTF_8);
        try {
            ExcelUtils.getInstance().exportObjects2Excel(datas, EasyLoanCompany.class, true, null, true, "company5.xlsx");
            System.out.println("导出完成");
        } catch (Excel4JException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}